package com.xcity.merge;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class OutputFile {
	private static final Logger LOG = LoggerFactory.getLogger(OutputFile.class);
	
	private static final String DEFAULT_FILENAME = "output.sql";
	private static final boolean DEFAULT_APPEND = true;
	private static PrintStream printStream = null;
	private static String fileName = DEFAULT_FILENAME;
	private static boolean append = DEFAULT_APPEND;
	
	private void init() throws Exception{
		File out = new File(fileName);
		if(!out.exists()){
			out.getParentFile().mkdirs();
			out.createNewFile();
		}
		
		printStream = new PrintStream(new FileOutputStream(fileName, append), true, "utf8");
	}
	
	public void setOut(String fName, boolean apd) throws Exception{
		fileName = fName;
		append = apd;
		try {
			init();
		} catch (Exception e) {
			e.printStackTrace();
			fileName = DEFAULT_FILENAME;
			append = DEFAULT_APPEND;
			
			init();
		}
	}
	
	public void println(String str){
		if(printStream == null){
			try {
				init();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		printStream.print(str);
	}
}
